package com.hardydou.data.service.impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.hardydou.common.result.ResultAssert;
import com.hardydou.data.mapper.FilmMapper;
import com.hardydou.data.model.Film;
import com.hardydou.data.service.IFilmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：hardy
 * @description：
 * @date ：Created in 2020/7/15
 * @Copyright ：Hardydou.com
 */
@Service
public class FilmServiceImpl implements IFilmService {

    @Autowired
    FilmMapper filmMapper;


    @Override
    public void add(Film film) {
        ResultAssert.notNull(film);
        filmMapper.insert(film);
    }

    @Override
    public void deleteById(Long resId) {
        Film film = new Film();
        film.setResId(resId);
        film.setDel(true);
        filmMapper.updateByPrimaryKeySelective(film);
    }

    @Override
    public void update(Film film) {
        filmMapper.updateByPrimaryKeySelective(film);
    }

    @Override
    public void updateWithBob(Film film) {
        filmMapper.updateByPrimaryKeyWithBLOBs(film);
    }

    @Override
    public Film findById(Long resId) {
        return filmMapper.selectByPrimaryKey(resId);
    }

    @Override
    public String findInfoById(Long resId) {
        return filmMapper.selectInfoById(resId);
    }

    @Override
    public List<Film> list(Map params) {
        return filmMapper.list(params);
    }


    @Override
    public List<Film> findByType(String fileType, int count) {
        Map params = new HashMap();
        params.put("type", fileType);
        PageHelper.startPage(1, count);
        return filmMapper.list(params);
    }

    @Override
    public List<Film> findLatest(int count) {

        Map params = new HashMap();
        PageHelper.startPage(1, count, "udate desc ");
        return filmMapper.list(params);
    }

    @Override
    public List<Film> findHots(int count) {
        Map params = new HashMap();
        params.put("stime", DateUtil.formatDate(DateUtil.lastMonth()));
        PageHelper.startPage(1, count, "score desc ");
        return filmMapper.list(params);
    }
}
